package com.zhmksy.jwt_study.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.jmx.snmp.SnmpUnknownModelLcdException;
import com.zhmksy.jwt_study.entity.User;
import com.zhmksy.jwt_study.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author zhmsky
 * @date 2021/12/4 16:08
 */
@Controller
@EnableTransactionManagement
@Slf4j

public class LoginController {
    @Autowired
    private UserService userService;

    @GetMapping("/")
    public String toLogin(){
        return "login";
    }

    @GetMapping("/login")
    public String login(String username, String password, Model model){
        log.debug("-------------------进入了");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name",username)
               .eq("password",password);
        User user = userService.getOne(wrapper);
        if(user==null){
            model.addAttribute("msg","用户名或密码错误!");
            //登录失败
            return "login";
        }else {
            //登录成功
            return "index";
        }
    }
}
